package com.alibaba.citrus.ots.sales.plan.repository;

import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.ots.sales.plan.converter.PlanScItemExtendConverter;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.QueryPlanScItemExtendRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.QuerySalesPlanDetailRequest;
import com.alibaba.citrus.ots.sales.plan.model.SalesPlanScItemStatusEnum;
import com.alibaba.citrus.ots.sales.plan.model.features.SalesPlanDetailFeatures;
import com.alibaba.citrus.ots.sales.plan.model.features.SalesPlanScItemFeatures;
import com.alibaba.citrus.ots.sales.plan.repository.request.PageQueryPlanScItemExtendRepoRequest;
import com.alibaba.citrus.ots.sales.plan.repository.request.QueryPlanScItemExtendCountRequest;
import com.alibaba.citrus.ots.sales.plan.sdo.SalesPlanDetailSDO;
import com.alibaba.citrus.ots.sales.plan.sdo.SalesPlanPublishSDO;
import com.alibaba.citrus.ots.sales.plan.sdo.SalesPlanScItemExtendSDO;
import com.alibaba.cz.base.tool.StringUtil;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcots.sales_plan.planscitemextend.dto.BatchQueryRequest;
import com.epoch.app.bcots.sales_plan.planscitemextend.dto.BatchSavePlanScItemExtendRequest;
import com.epoch.app.bcots.sales_plan.planscitemextend.dto.BatchSavePlanScItemExtendResponse;
import com.epoch.app.bcots.sales_plan.planscitemextend.dto.ExtendStatQueryRequest;
import com.epoch.app.bcots.sales_plan.planscitemextend.dto.LoadListByBizTypeRequest;
import com.epoch.app.bcots.sales_plan.planscitemextend.dto.LoadListByBizTypeResponse;
import com.epoch.app.bcots.sales_plan.planscitemextend.dto.LoadPlanScItemExtendRequest;
import com.epoch.app.bcots.sales_plan.planscitemextend.dto.QueryScItemExtentCountRequest;
import com.epoch.app.bcots.sales_plan.planscitemextend.dto.QueryScItemExtentCountResponse;
import com.epoch.app.bcots.sales_plan.planscitemextend.dto.UpdatePlanScItemExtendResponse;
import com.epoch.app.bcots.sales_plan.planscitemextend.model.PlanScItemExtend;
import com.epoch.app.bcots.sales_plan.planscitemextend.service.PlanScItemExtendService;
import com.epoch.app.bcots.sales_plan.salesplandetail.dto.LoadSalesPlanDetailRequest;
import com.epoch.app.bcots.sales_plan.salesplandetail.dto.UpdateSalesPlanDetailResponse;
import com.epoch.app.bcots.sales_plan.salesplandetail.model.SalesPlanDetail;
import com.epoch.app.bcots.sales_plan.salesplandetail.service.SalesPlanDetailService;
import com.google.common.collect.Lists;
import net.sf.json.JSONObject;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zls
 * @description:
 * @date 2021-10-15 11:12
 */
@Component
public class SalesPlanScItemExtendRepositoryImpl implements SalesPlanScItemExtendRepository {
    @Resource
    PlanScItemExtendService planScItemExtendService;

    @Resource
    SalesPlanDetailService salesPlanDetailService;


    @Override
    @RepositoryInvoker(printLog = true)
    public PageResult<List<SalesPlanScItemExtendSDO>> pageQueryPlanScItemExtendList(PageQueryPlanScItemExtendRepoRequest pageQueryPlanScItemExtendRequest) {
        LoadListByBizTypeRequest byBizTypeRequest = new LoadListByBizTypeRequest();
        BeanUtils.copyProperties(pageQueryPlanScItemExtendRequest,byBizTypeRequest);
        byBizTypeRequest.setBizIdList(pageQueryPlanScItemExtendRequest.getBizId());
        byBizTypeRequest.setScItemCategoryId(pageQueryPlanScItemExtendRequest.getScItemLeafCategoryId());
        LoadListByBizTypeResponse loadListByBizTypeResponse = planScItemExtendService.loadListByBizType(byBizTypeRequest);

        List<SalesPlanScItemExtendSDO> collect = loadListByBizTypeResponse.getResult().stream().map(planScItem -> {
            SalesPlanScItemExtendSDO salesPlanScItemExtendSDO = new SalesPlanScItemExtendSDO();
            BeanUtils.copyProperties(planScItem, salesPlanScItemExtendSDO);
            salesPlanScItemExtendSDO.calculatePlanSatisfaction2();
            // salesPlanScItemExtendSDO.calculateTaskSatisfaction();
            try {
                if (StringUtil.isNotEmpty(planScItem.getFeatures())){
                    SalesPlanScItemFeatures salesPlanScItemFeatures = (SalesPlanScItemFeatures) JSONObject.toBean(JSONObject.fromObject(salesPlanScItemExtendSDO.getFeatures()), SalesPlanScItemFeatures.class);
                    salesPlanScItemFeatures.setStatusDesc(SalesPlanScItemStatusEnum.getDesc(salesPlanScItemExtendSDO.getStatus()));
                    salesPlanScItemExtendSDO.setSalesPlanScItemFeatures(salesPlanScItemFeatures);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return salesPlanScItemExtendSDO;
        }).collect(Collectors.toList());
        return PageResult.of(collect, loadListByBizTypeResponse.getTotal());
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public List<SalesPlanScItemExtendSDO> batchQueryPlanScItemExtend(QueryPlanScItemExtendRequest queryPlanScItemExtendRequest) {
        BatchQueryRequest batchQueryRequest = new BatchQueryRequest();
        BeanUtils.copyProperties(queryPlanScItemExtendRequest, batchQueryRequest);
        batchQueryRequest.setPlanId(Objects.toString(queryPlanScItemExtendRequest.getPlanId(), null));
        batchQueryRequest.setModel(Objects.toString(queryPlanScItemExtendRequest.getModel(), null));
        List<String> bizIds = new ArrayList<>();
        if (StringUtils.isNotBlank(queryPlanScItemExtendRequest.getBizId())) {
            bizIds.add(queryPlanScItemExtendRequest.getBizId());
        }
        batchQueryRequest.setBizIds(bizIds);
        batchQueryRequest.setIds(queryPlanScItemExtendRequest.getIds());
        batchQueryRequest.setBizType(Objects.toString(queryPlanScItemExtendRequest.getBizType(), null));
        if (null != queryPlanScItemExtendRequest.getModelId()) {
            batchQueryRequest.setModelIds(Lists.newArrayList(queryPlanScItemExtendRequest.getModelId()));
        }
        Result<List<PlanScItemExtend>> scItemExtendList = planScItemExtendService.batchQuery(batchQueryRequest);
        return scItemExtendList.getResult().stream().map(scItem -> {
            SalesPlanScItemExtendSDO salesPlanScItemExtendSDO = new SalesPlanScItemExtendSDO();
            BeanUtils.copyProperties(scItem, salesPlanScItemExtendSDO);
            return salesPlanScItemExtendSDO;
        }).collect(Collectors.toList());
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public Boolean batchCreatePlanScItemExtend(List<SalesPlanScItemExtendSDO> requestList) {
        if (CollectionUtils.isEmpty(requestList)) {
            return true;
        }
        BatchSavePlanScItemExtendRequest batchSavePlanScItemExtendRequest = new BatchSavePlanScItemExtendRequest();
        batchSavePlanScItemExtendRequest.setRequest(PlanScItemExtendConverter.planScItemExtendSdoToDo(requestList));
        BatchSavePlanScItemExtendResponse batchSavePlanScItemExtendResponse = planScItemExtendService.batchSavePlanScItemExtend(batchSavePlanScItemExtendRequest);
        return batchSavePlanScItemExtendResponse != null && batchSavePlanScItemExtendResponse.getCount() != null && batchSavePlanScItemExtendResponse.getCount() == requestList.size();
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public Boolean editSalesPlanScItemExtend(SalesPlanScItemExtendSDO salesPlanScItemExtendSDO) {
        PlanScItemExtend planScItemExtend = new PlanScItemExtend();
        BeanUtils.copyProperties(salesPlanScItemExtendSDO, planScItemExtend);
        planScItemExtend.setGmtModified(new Date());
        UpdatePlanScItemExtendResponse updatePlanScItemExtendResponse = planScItemExtendService.updatePlanScItemExtend(planScItemExtend);
        return updatePlanScItemExtendResponse.getCount() > 0;
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public Integer edit(SalesPlanPublishSDO salesPlanPublishSDO) {
        PlanScItemExtend planScItemExtend = new PlanScItemExtend();
        BeanUtils.copyProperties(salesPlanPublishSDO, planScItemExtend);
        UpdatePlanScItemExtendResponse response = planScItemExtendService.updatePlanScItemExtend(planScItemExtend);
        return response.getCount();
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public SalesPlanDetailSDO querySalesPlanDetail(QuerySalesPlanDetailRequest querySalesPlanDetailRequest) {
        LoadSalesPlanDetailRequest loadSalesPlanDetailRequest = new LoadSalesPlanDetailRequest();
        BeanUtils.copyProperties(querySalesPlanDetailRequest, loadSalesPlanDetailRequest);
        SalesPlanDetail salesPlanDetail = salesPlanDetailService.loadSalesPlanDetail(loadSalesPlanDetailRequest);
        SalesPlanDetailSDO salesPlanDetailSDO = SalesPlanDetailSDO.builder().build();
        BeanUtils.copyProperties(salesPlanDetail, salesPlanDetailSDO);
        SalesPlanDetailFeatures salesPlanDetailFeatures = (SalesPlanDetailFeatures) JSONObject.toBean(JSONObject.fromObject(salesPlanDetailSDO.getFeatures()), SalesPlanDetailFeatures.class);
        salesPlanDetailSDO.setSalesPlanDetailFeatures(salesPlanDetailFeatures);
        return salesPlanDetailSDO;
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public Integer editSalesPlanDetail(SalesPlanDetailSDO salesPlanDetailSDO) {
        SalesPlanDetail salesPlanDetail = new SalesPlanDetail();
        BeanUtils.copyProperties(salesPlanDetailSDO, salesPlanDetail);
        UpdateSalesPlanDetailResponse response = salesPlanDetailService.updateSalesPlanDetail(salesPlanDetail);
        return response.getCount();
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public Integer queryPlanScItemExtendCount(QueryPlanScItemExtendCountRequest request) {
        QueryScItemExtentCountRequest countRequest = new QueryScItemExtentCountRequest();
        countRequest.setPlanId(request.getPlanId());
        countRequest.setPoolType(request.getPoolType());
        countRequest.setBizType(request.getBizType());
        countRequest.setStatus(request.getStatus());
        QueryScItemExtentCountResponse queryScItemExtentCountResponse = planScItemExtendService.queryScItemExtentCount(countRequest);
        if (queryScItemExtentCountResponse != null){
            return queryScItemExtentCountResponse.getCount();
        }
        return null;
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public SalesPlanScItemExtendSDO queryPlanScItemExtendSum(PageQueryPlanScItemExtendRepoRequest extendRepoRequest) {
        ExtendStatQueryRequest queryRequest = new ExtendStatQueryRequest();
        queryRequest.setPlanId(extendRepoRequest.getPlanId());
        queryRequest.setPoolType(extendRepoRequest.getPoolType());
        queryRequest.setBizType(extendRepoRequest.getBizType());
        queryRequest.setBizIdList(extendRepoRequest.getBizId());
        queryRequest.setScItemId(extendRepoRequest.getScItemId());
        queryRequest.setScItemLeafCategoryId(extendRepoRequest.getScItemLeafCategoryId());
        queryRequest.setModel(extendRepoRequest.getModel());
        queryRequest.setModelId(extendRepoRequest.getModelId());
        PlanScItemExtend planScItemExtend = planScItemExtendService.extendStatQuery(queryRequest);
        if (planScItemExtend != null){
            SalesPlanScItemExtendSDO result = new SalesPlanScItemExtendSDO();
            BeanUtils.copyProperties(planScItemExtend,result);
            return result;
        }
        return null;
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public SalesPlanScItemExtendSDO queryById(Long id) {
        LoadPlanScItemExtendRequest request = new LoadPlanScItemExtendRequest();
        request.setId(id);
        PlanScItemExtend planScItemExtend = planScItemExtendService.loadPlanScItemExtend(request);
        if (planScItemExtend != null){
            SalesPlanScItemExtendSDO result = new SalesPlanScItemExtendSDO();
            BeanUtils.copyProperties(planScItemExtend,result);
            return result;
        }
        return null;
    }
}
